<?php
/**
 * 个人中心
 * User: Administrator
 * Date: 2022/3/15
 * Time: 15:20
 */

namespace app\api\controller;
use app\common\controller\Api;
use think\Db;
use think\Request;

class PersonalCenter extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = '*';

    /**
     * 我的收藏
     */
    public function myCollect()
    {
        if (!Request::instance()->isGet())
            $this->error('请求方式不正确');
        $page = input('post.page',1);
        $limit = input('post.limit',10);
        $user_id = $this->auth->id;
        $list = Db::name('collect')->alias('a')
            ->join('programme b','a.programme_id=b.id')
            ->order('a.id','desc')
            ->field('b.id,b.title,b.image')
            ->where(['a.user_id'=>$user_id])
            ->paginate($limit,false,['page'=>$page]);
        if($list)
            $this->success('',$list);
        $this->error('暂无数据');
    }

    /**
     * 我的订单
     */
    public function order()
    {
        if (!Request::instance()->isGet())
            $this->error('请求方式不正确');

        $page = input('get.page',1);
        $limit = input('get.limit',10);

        $start_time = input('get.start_time');

        $end_time = input('get.end_time');
        $user_id = $this->auth->id;

        $where = [];
        $where['user_id'] = $user_id;


        $query = Db::name('order')->order('id','desc')->field('*,FROM_UNIXTIME(createtime,"%Y-%m-%d  %H:%i:%s") as time')->where($where);
        if($start_time && $end_time)
        {
            $query->whereTime('createtime','between',[$start_time,$end_time]);
        }
        $list = $query->paginate($limit,false,['page'=>$page]);
        $arr = [0=>'待支付',1=>'支付成功',2=>'支付失败'];
        $list = $list->toArray();
        if(!empty($list['data']))
        {

            foreach($list['data'] as $k=>$v)
            {
                unset($list['data'][$k]['order_business']);
                $list['data'][$k]['status_text'] = $arr[intval($v['status'])];
            }
            $this->success('',$list);
        }
        $this->error('暂无数据');
    }

    /**
     * 我的评价
     */
    public function myEvaluate()
    {
        if (!Request::instance()->isGet())
            $this->error('请求方式不正确');

        $page = input('get.page',1);
        $limit = input('get.limit',10);

        $user_id = $this->auth->id;
        $list = Db::name('evaluate')->alias('a')
            ->join('rm_programme b','a.programme_id=b.id')->field('a.*,b.title')->where('a.user_id',$user_id)->paginate($limit,false,['page'=>$page]);
        if($list)
            $this->success('',$list);
        $this->error('暂无数据');
    }

    /**
     * 我的方案
     */
    public function myProgramme()
    {
        if (Request::instance()->isGet())
        {
            $page = input('get.page',1);
            $limit = input('get.limit',10);

            $list = Db::name('programme')->field('id,title,image,price,status,FROM_UNIXTIME(createtime,"%Y-%m-%d") as time')->where(['type'=>2,'admin_id'=>$this->auth->id])->paginate($limit,false,['page'=>$page]);
            if($list)
                $this->success('',$list);
            $this->error('暂无数据');
        }
        else
        {
            $params = input('post.');

            $result = $this->validate($params,'MyProgramme');

            if (true !== $result) {
                // 验证失败 输出错误信息
                $this->error($result);
            }
            $time = time();
            $params['admin_id'] = $this->auth->id;
            $params['type'] = 2;
            $params['createtime'] = $time;
            $params['updatetime'] = $time;

            $res = Db::name('programme')->insert($params);
            if($res)
                $this->success('发布成功');
            $this->error('发布失败');
        }
    }

    /**
     * 我的积分
     */
    public function myScore()
    {
        if (!Request::instance()->isGet())
            $this->error('请求方式不正确');
        $page = input('get.page',1);
        $limit = input('get.limit',10);
        $list = Db::name('user_score_log')->field('*,FROM_UNIXTIME(createtime,"%Y-%m-%d") as time')->order('id','desc')->where('user_id',$this->auth->id)->paginate($limit,false,['page'=>$page]);
        if($list)
            $this->success('',$list);
        $this->error('暂无数据');
    }

    /**
     * 我的账单
     */
    public function bill()
    {
        if (!Request::instance()->isGet())
            $this->error('请求方式不正确');
        $page = input('get.page',1);
        $limit = input('get.limit',10);
        $list = Db::name('bill')->field('*,FROM_UNIXTIME(createtime,"%Y-%m-%d") as time')->where('user_id',$this->auth->id)->paginate($limit,false,['page'=>$page]);
        if($list)
            $this->success('',$list);
        $this->error('暂无数据');

    }

    /**
     * 我的图库
     */
    public function gallery()
    {
        if (!Request::instance()->isGet())
            $this->error('请求方式不正确');
        $page = input('get.page',1);
        $limit = input('get.limit',10);

        $list = Db::name('order')->alias('a')
                    ->join('rm_programme b','a.programme_id = b.id')
                    ->field('b.id,b.title,b.image')
                    ->where(['a.type'=>3,'a.user_id'=>$this->auth->id,'state'=>2,'status'=>1])
                    ->paginate($limit,false,['page'=>$page]);
        if($list)
            $this->success('',$list);
        $this->error('暂无数据');
    }

    /**
     * 图片列表
     */
    public function images()
    {
        if (!Request::instance()->isGet())
            $this->error('请求方式不正确');
        if(!$programme_id = input('get.programme_id'))
            $this->error('缺少参数');

        $info = Db::name('pictures')->where('rm_programme_id',$programme_id)->find();
        if(!$info)
            $this->error('暂无数据');
        $info['imagesArr'] = [];
        if($info['images'])
            $info['imagesArr'] = explode(',',$info['images']);

        $this->success('',$info);
    }
}